﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using FileShredder;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FileShredderUI
{
    public partial class FileShredderWindow : Window
    {
        private TypeReplace _typeReplace;
        public FileShredderWindow()
        {
            _typeReplace = TypeReplace.High;
            InitializeComponent();
        }
         
        private void AddFile(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Multiselect = true;
            dialog.ShowDialog();
            if (dialog.FileNames != null)
            {
                foreach (var fileName in dialog.FileNames)
                    listView.Items.Add(new FileInfo(fileName));
            }
        }

        private void ReplaceAndDeleteFile(object sender, RoutedEventArgs e)
        {
            progressBar.Value = 0;
            progressBar.Maximum = listView.Items.Count > 0 ? listView.Items.Count : 100;
            var listComplitedDelete = new List<FileInfo>();
            bool existEx = false;
            foreach (FileInfo item in listView.Items)
            {
                try
                {
                    var shreder = new ShredderFile(item);
                    shreder.ReplaceFile(_typeReplace);
                    shreder.DeleteFile();
                    listComplitedDelete.Add(item);                    
                }
                catch { existEx = true; }
                progressBar.Value += 1;
            }            

            if (existEx)
            {
                foreach (FileInfo deletedFiles in listComplitedDelete)
                {
                    if (listView.Items.Contains(deletedFiles))
                    {
                        int indexItem = listView.Items.IndexOf(deletedFiles);
                        listView.Items.RemoveAt(indexItem);
                    }
                }
            }
            else
                listView.Items.Clear();                       
        }

        private void OnlyReplaceFile(object sender, RoutedEventArgs e)
        {
            progressBar.Value = 0;        
            progressBar.Maximum = listView.Items.Count;
            
            foreach (FileInfo item in listView.Items)
            {
                try
                {
                    var shreder = new ShredderFile(item);
                    shreder.ReplaceFile(_typeReplace);                    
                }
                catch
                { }
                progressBar.Value += 1;
            }           
            
        }
        
        private void ClearList(object sender, RoutedEventArgs e)
        {
            progressBar.Value = 0;
            progressBar.Maximum = 100;
            listView.Items.Clear();
        }

        private void radioButton_Checked_High(object sender, RoutedEventArgs e)
        {
            _typeReplace = TypeReplace.High;
        }

        private void radioButton_Checked_Middle(object sender, RoutedEventArgs e)
        {
            _typeReplace = TypeReplace.Middle;
        }

        private void radioButton_Checked_Simple(object sender, RoutedEventArgs e)
        {
            _typeReplace = TypeReplace.Simple;
        }
    }
}
